package com.example.moilapp.service;

import org.json.JSONArray;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Moil_Package_Update_Service extends AsyncTask<String, Void, String>{

	TextView myView;
	TextView myView_title;
	TextView myView_pic;
	TextView myView_pic_data;
	
	//private ProgressDialog progdialod;
	private Activity activity;
	
	private String jobid;
	private String pickup_delivery;
	
	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket) 
	private int timeoutSocket = 45000;
	
	private static byte[] buff =new byte[1024];
	private String retval="";

	private static final String SOAP_ACTION = "http://tempuri.org/GetPackageCount";

	//http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx
	
    private static final String METHOD_NAME = "GetPackageCount";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx";
	private static final String TAG = null;

    public Moil_Package_Update_Service(TextView bmImage,TextView bmtitle,TextView myView_pic,TextView myView_pic_data,String jobid,String pickup_delivery) {
        this.myView = bmImage;
        this.myView_title = bmtitle;
        this.jobid = jobid;
        this.pickup_delivery = pickup_delivery;
        this.myView_pic_data = myView_pic_data;
        this.myView_pic = myView_pic;
        Log.e("jobid", jobid);
    }
    
    @Override
    protected void onPreExecute() {
    	super.onPreExecute();
    	//progdialod = ProgressDialog.show(this.activity, "Package Number", "Loading, Please Wait",true,false);
    }

    protected String doInBackground(String... urls) {
    	 try {

	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	            request.addProperty("JobID", jobid);

	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	            envelope.dotNet=true;
	            envelope.setOutputSoapObject(request);

	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            
	            SoapPrimitive resultSoapPrimitive;
	            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
	                
	            String result = resultSoapPrimitive.toString();

	            retval = result;
	            
	            Log.d("return", retval);
	            
	            return retval;
	           
	        } catch (Exception e) {
	            
	       }
		 return retval;
    }

    protected void onPostExecute(String result) {
    	
    	Log.d("Post Package", result);
		//progdialod.dismiss();

		Log.d("Post Exec", result);
    	
		
		myView.setText(result);
		
		if(pickup_delivery.equals("1")){
			this.myView_title.setVisibility(View.VISIBLE);
			this.myView.setVisibility(View.VISIBLE);
			this.myView_pic_data.setVisibility(View.VISIBLE);
			this.myView_pic.setVisibility(View.VISIBLE);
		}else{
			this.myView_title.setVisibility(View.GONE);
			this.myView.setVisibility(View.GONE);
			this.myView_pic_data.setVisibility(View.GONE);
			this.myView_pic.setVisibility(View.GONE);
		}
		
    }
}
